#include "gui_app.h"
#include "sensor_data_save.h"
#include "rtc_driver.h"
#include "adc_driver.h"
#include "iot_driver.h"
#include "sb_gateway.h"

char* cancel_red_alarm_hent = "正在执行红色水位预警,请按ENTER键取消本轮预警";
char* cancel_yellow_alarm_hent = "正在执行黄色水位预警,请按ENTER键取消本轮预警";
char* cancel_rise_alarm_hent = "正在执行涨速红色预警,请按ENTER键取消本轮预警";
char* attention_alarm_hent = "触发关注水位预警,将减小休眠间隔到1分钟";
char* normal_hent = "运行中,当前无预警";


// 系统信息框
lv_obj_t* sys_info_obj = NULL;
// 传感器信息框
lv_obj_t* snsr_info_obj = NULL;
lv_obj_t* scr = NULL;
// 系统信息标签
lv_obj_t* info_label = NULL;

// 电表标签
lv_obj_t* meter_label = NULL;
// 
lv_obj_t* starer_label = NULL;

lv_obj_t* snsr_state_label_1 = NULL;
lv_obj_t* snsr_state_label_2 = NULL;
lv_obj_t* snsr_data_title_1 = NULL;
lv_obj_t* snsr_data_title_2 = NULL;
lv_obj_t* snsr_data_label_1 = NULL;
lv_obj_t* snsr_data_label_2 = NULL;


//传感器状态led
lv_obj_t* snsr_led_1 = NULL;
lv_obj_t* snsr_led_2 = NULL;


//传感器通道容器
lv_obj_t* ch_obj_0 = NULL;
lv_obj_t* ch_obj_1 = NULL;

// 日志和网络状态容器
lv_obj_t* log_obj_0 = NULL;

// 数据展示容器
lv_obj_t* data_obj_0 = NULL;

lv_obj_t* srv_led_0 = NULL;
lv_obj_t* srv_led_1 = NULL;
lv_obj_t* srv_led_title_1 = NULL;
lv_obj_t* srv_led_title_2 = NULL;
lv_obj_t* log_label_1 = NULL;

lv_obj_t* line0 = NULL;
lv_obj_t* line1 = NULL;
lv_obj_t* line2 = NULL;
lv_obj_t* line3 = NULL;
lv_obj_t* line4 = NULL;
lv_obj_t* line5 = NULL;
lv_obj_t* line6 = NULL;

lv_obj_t* test_label_1 = NULL;

lv_obj_t* data_table1 = NULL;



lv_timer_t* lv_timer1 = NULL;



// 定时器回调
static void lv_timer1_cb(lv_timer_t* timer)
{
	static uint32_t cnt = 0;
	static uint8_t circle = 0;


	


	cnt++;
	if (cnt % 5 == 0)
	{
		
		
		circle++;

		float rain_5m = 0;			// 5分钟雨量
		float rain_total = 0;		// 总雨量
		float current_level = 0;		// 当前液位
		float current_height = 0;	// 当前空高
		float current_elevation = 0;	// 当前高程
		float attention_level = 0;	// 关注水位
		float yellow_level = 0;		// 黄色水位
		float red_level = 0;			// 红色水位
		float current_rise_speed = 0;
		float rise_speed = 0;		// 涨速阈值



		char rain_5m_str[20] = { 0 };			// 5分钟雨量
		char rain_total_str[20] = { 0 };		// 总雨量
		char current_level_str[20] = { 0 };		// 当前液位
		char current_height_str[20] = { 0 };	// 当前空高
		char current_elevation_str[20] = { 0 };	// 当前高程
		char attention_level_str[20] = { 0 };	// 关注水位
		char yellow_level_str[20] = { 0 };		// 黄色水位
		char red_level_str[20] = { 0 };			// 红色水位
		char current_rise_speed_str[20] = { 0 };// 当前涨速
		char rise_speed_str[20] = { 0 };		// 涨速阈值

		rain_5m = get_alarm_current_5m_rain();
		rain_total = get_alarm_current_total_rain();
		current_level = get_alarm_current_level();
		current_height = get_alarm_current_height();
		current_elevation = get_alarm_elevation();
		attention_level = get_alarm_attention_level();
		yellow_level = get_alarm_yellow_level();
		red_level = get_alarm_red_level();
		current_rise_speed = get_alarm_current_rise_speed();
		rise_speed = get_alarm_rise_rate();


		sprintf(rain_5m_str, "%.3f毫米", rain_5m);
		sprintf(rain_total_str, "%.3f毫米", rain_total);
		sprintf(current_level_str, "%.3f米", current_level);
		sprintf(current_height_str, "%.3f米", current_height);
		sprintf(current_elevation_str, "%.3f米", current_elevation);
		sprintf(attention_level_str, "%.3f米", attention_level);
		sprintf(yellow_level_str, "%.3f米", yellow_level);
		sprintf(red_level_str, "%.3f米", red_level);
		sprintf(rise_speed_str, "%.3f米/分", rise_speed);
		sprintf(current_rise_speed_str, "%.3f米/分", current_rise_speed);


		lv_table_set_cell_value_fmt(data_table1, 1, 0, "%s", rain_5m_str);
		lv_table_set_cell_value_fmt(data_table1, 1, 1, "%s", rain_total_str);
		lv_table_set_cell_value_fmt(data_table1, 1, 2, "%s", current_level_str);
		lv_table_set_cell_value_fmt(data_table1, 1, 3, "%s", current_height_str);
		lv_table_set_cell_value_fmt(data_table1, 1, 4, "%s", current_rise_speed_str);
		
		lv_table_set_cell_value_fmt(data_table1, 3, 0, "%s", current_elevation_str);
		lv_table_set_cell_value_fmt(data_table1, 3, 1, "%s", attention_level_str);
		lv_table_set_cell_value_fmt(data_table1, 3, 2, "%s", yellow_level_str);
		lv_table_set_cell_value_fmt(data_table1, 3, 3, "%s", red_level_str);
		lv_table_set_cell_value_fmt(data_table1, 3, 4, "%s", rise_speed_str);
		
		circle = circle % 5;

	}
	
	if (1)
	{
		uint16_t year;
		uint8_t month, day, hour, minute, second;
		char ccid[30] = { 0 };

		
		char temp[20] = { 0 };
		char imei[20] = { 0 };
		uint8_t rsi = 0;
		uint8_t gtcsq = 0;
		char volt[20] = { 0 };
		uint32_t devAdr = 0;

		devAdr = get_netway_gb_devAdr();
		sprintf(temp, "网关信号:%d", 12);
		get_time(&year, &month, &day, &hour, &minute, &second);
		gtcsq = get_netway_signal();
		sprintf(volt, "电压:%.2f", get_power_voltage());

		iot_info_get(ccid, imei, &rsi);
		if (strlen(ccid) == 0)
		{
			sprintf(ccid, "01234567890123456789");
		}

		if (devAdr == 0)
		{
			devAdr = 12345678;
		}
		lv_label_set_text_fmt(info_label, "版本:%s，  国标ID:%-8d，    时间:%d_%d_%d\nCCID:%20s，   %s，  CSQ:%2d，  %s",
			SOFT_VERSION, 0, hour, minute, second, ccid, volt, rsi, temp);
	}

	if (is_iot_channel_connected(0) == true)
	{
		lv_led_set_color(srv_led_0, lv_color_hex(0x00FF00));
		if (is_iot_channel_idle(0) == true)
		{
			lv_led_set_color(srv_led_0, lv_color_hex(0xFFFF00));
		}
	}
	if (is_iot_channel_connected(1) == true)
	{
		lv_led_set_color(srv_led_1, lv_color_hex(0x00FF00));
		if (is_iot_channel_idle(1) == true)
		{
			lv_led_set_color(srv_led_1, lv_color_hex(0xFFFF00));
		}
	}


	
}
//启动GUI系统
void gui_app_start()
{

	uint16_t year;
	uint8_t month, day, hour, minute, second;
	char ccid[25] = { 0 };
	char imei[20] = { 0 };
	uint8_t rsi = 0;
	char volt[20] = { 0 };
	char temp[20] = { 0 };
	sprintf(volt, "电压:%.2f", get_power_voltage());
	sprintf(temp, "网关信号:%d", 12);
	iot_info_get(ccid, imei, &rsi);

	get_time(&year, &month, &day, &hour, &minute, &second);

	scr = lv_scr_act();
	sys_info_obj = lv_obj_create(scr);
	lv_obj_set_size(sys_info_obj, 480, 40);
	lv_obj_set_style_bg_color(sys_info_obj, lv_color_hex(0x778899), LV_STATE_DEFAULT);

	info_label = lv_label_create(scr);
	//lv_obj_set_size(info_label, 480, 40);
	lv_obj_set_align(info_label, LV_ALIGN_TOP_LEFT);
	// 显示版本号 日期，时间，信号，电压，温度，ccid
	lv_label_set_text_fmt(info_label, "版本:%s，  国标ID:%-8d，   时间:%d_%d_%d\nCCID:%20s，   %s，  CSQ:%2d， %s",
		SOFT_VERSION,0, hour, minute, second,"01234567890123456789", volt, rsi, temp);
	lv_label_set_long_mode(info_label, LV_LABEL_LONG_CLIP);

	
	

	data_obj_0 = lv_obj_create(scr);
	lv_obj_set_size(data_obj_0, 480, 200);
	lv_obj_align_to(data_obj_0, sys_info_obj, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);

	//lv_obj_set_style_bg_color(data_obj_0, lv_color_hex(0x1E90FF), LV_STATE_DEFAULT);





	
	lv_obj_set_style_pad_all(data_obj_0,0, LV_STATE_DEFAULT);
	lv_obj_update_layout(data_obj_0);


	data_table1 = lv_table_create(data_obj_0);
	lv_obj_set_style_text_font(data_table1, &myfont_3,LV_STATE_DEFAULT);
	//lv_obj_set_style_pad_all(data_table1, 0, LV_STATE_DEFAULT);
	lv_table_set_col_cnt(data_table1,5);
	lv_table_set_row_cnt(data_table1, 4);
	lv_table_set_col_width(data_table1, 0, 96);
	lv_table_set_col_width(data_table1, 1, 96);
	lv_table_set_col_width(data_table1, 2, 96);
	lv_table_set_col_width(data_table1, 3, 96);
	lv_table_set_col_width(data_table1, 4, 96);
	lv_table_set_cell_value(data_table1, 0, 0, "5分钟雨量");
	lv_table_set_cell_value(data_table1, 0, 1, "雨量总量");
	lv_table_set_cell_value(data_table1, 0, 2, "当前水位");
	lv_table_set_cell_value(data_table1, 0, 3, "当前空高");
	lv_table_set_cell_value(data_table1, 0, 4, "当前涨速");


	//lv_table_set_cell_value_fmt(data_table1,1,0,"%d",3);
	lv_table_set_cell_value(data_table1, 1, 0, "0");
	lv_table_set_cell_value(data_table1, 1, 1, "0");
	lv_table_set_cell_value(data_table1, 1, 2, "0");
	lv_table_set_cell_value(data_table1, 1, 3, "0");
	lv_table_set_cell_value(data_table1, 1, 4, "0");

	lv_table_set_cell_value(data_table1, 2, 0, "当前高程");
	lv_table_set_cell_value(data_table1, 2, 1, "关注水位");
	lv_table_set_cell_value(data_table1, 2, 2, "黄色水位");
	lv_table_set_cell_value(data_table1, 2, 3, "红色水位");
	lv_table_set_cell_value(data_table1, 2, 4, "涨速阈值");

	lv_table_set_cell_value(data_table1, 3, 0, "0");
	lv_table_set_cell_value(data_table1, 3, 1, "0");
	lv_table_set_cell_value(data_table1, 3, 2, "0");
	lv_table_set_cell_value(data_table1, 3, 3, "0");
	lv_table_set_cell_value(data_table1, 3, 4, "0");




	

	



	


	
	// 日志显示容器对象
	log_obj_0 = lv_obj_create(scr);
	lv_obj_set_style_pad_all(log_obj_0, 5, LV_STATE_DEFAULT);
	lv_obj_set_size(log_obj_0, 480, 80);
	lv_obj_set_style_bg_color(log_obj_0, lv_color_hex(0x00BFFF), LV_STATE_DEFAULT);
	lv_obj_align(log_obj_0, LV_ALIGN_BOTTOM_LEFT, 0, 0);

	srv_led_title_1 = lv_label_create(log_obj_0);
	//lv_obj_set_size(srv_led_title_1, 40, 40);

	lv_obj_align(srv_led_title_1, LV_ALIGN_TOP_LEFT, 0,0 );
	lv_label_set_text(srv_led_title_1, "svr0:");
	srv_led_0 = lv_led_create(log_obj_0);
	lv_obj_set_size(srv_led_0, 20, 10);
	lv_obj_align_to(srv_led_0, srv_led_title_1, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
	lv_led_set_color(srv_led_0, lv_color_hex(0xFF0000));
	//lv_led_set_brightness(srv_led_0, LV_LED_BRIGHT_MAX);

	srv_led_title_2 = lv_label_create(log_obj_0);
	//lv_obj_set_size(srv_led_title_1, 40, 40);

	lv_obj_align(srv_led_title_2, LV_ALIGN_BOTTOM_LEFT, 0, 0);
	lv_label_set_text(srv_led_title_2, "svr1:");
	srv_led_1 = lv_led_create(log_obj_0);
	lv_obj_set_size(srv_led_1, 20, 10);
	lv_obj_align_to(srv_led_1, srv_led_title_2, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
	lv_led_set_color(srv_led_1, lv_color_hex(0xFF0000));
	//lv_led_set_brightness(srv_led_1, LV_LED_BRIGHT_MAX);

	log_label_1 = lv_label_create(log_obj_0);
	lv_obj_set_size(log_label_1, 360, 60);
	lv_obj_set_style_bg_color(log_label_1, lv_color_hex(0xFFE4E1), LV_STATE_DEFAULT);
	lv_obj_set_style_bg_opa(log_label_1, 150, LV_PART_MAIN);
	lv_obj_align(log_label_1, LV_ALIGN_TOP_RIGHT, 0, 0);
	lv_label_set_text(log_label_1, "运行中...");
	//lv_obj_align_to(log_label_1, srv_led_0, LV_ALIGN_OUT_RIGHT_TOP, 0, -10);

	lv_timer1 = lv_timer_create(lv_timer1_cb, 1000, NULL);




}


#if 0
//类似于android系统中的toast提示
//msg:提示消息
//time:多长后自动关闭消息,单位ms
void toast(const char* msg, uint16_t time)
{
	lv_obj_t* parent = lv_scr_act();
	lv_obj_t* mbox = lv_mbox_create(parent, NULL);
	lv_obj_set_width(mbox, lv_obj_get_width(parent) * 0.7f);//设置宽度
	lv_label_set_recolor(((lv_mbox_ext_t*)lv_obj_get_ext_attr(mbox))->text, true);//使能文本内容的重绘色功能
	lv_mbox_set_text(mbox, msg);//设置消息
	lv_obj_align(mbox, NULL, LV_ALIGN_IN_TOP_MID, 0, 30);//设置顶部对齐
	lv_mbox_start_auto_close(mbox, time);//多长时间后自动关闭
}


#define BACK_BTN_TITLE		LV_SYMBOL_LEFT" Back\n"   //返回按钮的文本内容,加\n是为了加大点击区域
//返回按钮专用的事件回调函数
static void back_btn_event_handler(lv_obj_t* obj, lv_event_t event)
{
	if (event == LV_EVENT_PRESSED)//按下事件
	{
		lv_label_set_text(obj, "#444444 "BACK_BTN_TITLE"#");//使能文本颜色变灰,有按下的效果
		lv_label_set_recolor(obj, true);//使能重绘色
	}
	else if (event == LV_EVENT_RELEASED || event == LV_EVENT_PRESS_LOST)//松手事件或划出控件有效区域事件
	{
		//删除窗体,其实也就是删除lv_page对象
		//因为back_btn是在lv_page页面中的,需要获取2层才能获取到真正的lv_page对象
		obj = lv_obj_get_parent(obj);
		obj = lv_obj_get_parent(obj);//此时的obj为lv_page页面对象,即窗体
		lv_event_cb_t event_cb = lv_obj_get_event_cb(obj);
		if (event_cb)//此窗体是否设置了事件回调函数,设置了的话则调用,可以用来进行释放资源等操作
			event_cb(obj, USER_EVENT_BACK);
		lv_obj_del(obj);//删除此窗体,相当于返回操作 
	}
}

//创建一个通用的窗体,它具有标题和返回按钮
//parent:父对象
//title:窗体的标题
lv_obj_t* general_win_create(lv_obj_t* parent, const char* title)
{
#define	TOP_OFFSET	5
	lv_obj_t* win;

	//创建窗体
	win = lv_page_create(parent, NULL);//用lv_page页面来模拟窗体
	lv_obj_set_size(win, lv_obj_get_width(parent), lv_obj_get_height(parent));//设置窗体的大小
	lv_page_set_sb_mode(win, LV_SB_MODE_AUTO);//设置滚动条的模式为自动
	lv_page_set_edge_flash(win, true);//使能边缘半圆弧动画效果
	lv_page_set_style(win, LV_PAGE_STYLE_SCRL, &lv_style_transp_tight);//设置scrl容器的样式为透明
	//创建一个标题
	lv_obj_t* title_label = lv_label_create(win, NULL);
	lv_label_set_text(title_label, title);//设置窗体标题
	lv_obj_align(title_label, NULL, LV_ALIGN_IN_TOP_MID, 0, TOP_OFFSET);//设置对齐方式


	//创建返回按钮
	lv_obj_t* back_btn = lv_label_create(win, NULL);//用标签来模拟按钮
	lv_label_set_text(back_btn, BACK_BTN_TITLE);//设置文本
	lv_obj_align(back_btn, NULL, LV_ALIGN_IN_TOP_LEFT, 5, TOP_OFFSET);//设置对齐方式
	lv_obj_set_click(back_btn, true);//使能点击功能
	lv_obj_set_event_cb(back_btn, back_btn_event_handler);//设置事件回调函数,用于关闭窗体

	return win;
}

#endif







